package com.prs.crm.action.oa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.oa.RegulationType;
import com.prs.crm.service.oa.RegulationTypeService;
import com.prs.crm.service.sys.LogService;

public class RegulationTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private RegulationType regulationType;
	private RegulationTypeService regulationTypeService;
	private PaginationSupport pagination;
	private Integer[] regulationTypeIds;
	private Collection<TreeNode> nodes;
	private LogService logService;


	@Action("listRegulationType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RegulationType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRegulationTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "loadRegulationTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadRegulationTypeTree() {
		nodes = new ArrayList<TreeNode>();
		List<RegulationType> regs = this.getRegulationTypeService()
				.getAllRegulationType();
		for (RegulationType reg : regs) {
			TreeNode node = new TreeNode();
			node.setId(reg.getId().toString());
			node.setText(reg.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action("selectRegulationType")
	public String selectRegulationType() {
		return this.execute();
	}

	@Action("editRegulationType")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveRegulationType", results = {
			@Result(name = "success", type = "redirect", location = "listRegulationType"),
			@Result(name = "error", type = "chain", location = "editRegulationType") })
	public String save() {
		if (regulationType.getName() == null
				|| "".equals(regulationType.getName().trim())) {
			addActionError("类型名称不能为空");
			return ERROR;
		}	if (this.getLogService().getSearchName(RegulationType.class, regulationType
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getRegulationTypeService().save(regulationType);
		return SUCCESS;
	}

	@Action(value = "removeRegulationType", results = {
			@Result(name = "success", type = "redirect", location = "listRegulationType"),
			@Result(name = "error", type = "chain", location = "listRegulationType") })
	public String remove() {
		if (regulationTypeIds == null) {
			this.addActionError("请选择要删除的制度类型");
			return ERROR;
		}
		for (Integer id : regulationTypeIds) {
			RegulationType type = this.getRegulationTypeService().get(id);
			if (type.getRegulations().size() != 0) {
				this.addActionError("已有制度引用，不能删除");
				return ERROR;
			}
		}
		this.getRegulationTypeService().batchRemove(regulationTypeIds);
		return SUCCESS;
	}

	public RegulationType getRegulationType() {
		return regulationType;
	}

	public void setRegulationType(RegulationType regulationType) {
		this.regulationType = regulationType;
	}

	public RegulationTypeService getRegulationTypeService() {
		return regulationTypeService;
	}

	public void setRegulationTypeService(
			RegulationTypeService regulationTypeService) {
		this.regulationTypeService = regulationTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getRegulationTypeIds() {
		return regulationTypeIds;
	}

	public void setRegulationTypeIds(Integer[] regulationTypeIds) {
		this.regulationTypeIds = regulationTypeIds;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
